package com.whitehot.klib.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * @author Rabtman
 */

public class SystemUtils
{

    /**
     * 获取进程名称
     */
    public static String getCurProcessName()
    {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) Utils.getContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager != null)
        {
            for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                    .getRunningAppProcesses())
            {
                if (appProcess.pid == pid)
                {
                    return appProcess.processName;
                }
            }
        }
        return "";
    }

    /**
     * 获取versioncode
     *
     * @return
     */
    public static int getVersionCode(Context mContext)
    {
        PackageManager pm = mContext.getPackageManager();
        try
        {
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    /***
     * 获取版本名
     * @param mContext
     * @return
     */
    public static String getVersionName(Context mContext)
    {
        PackageManager pm = mContext.getPackageManager();
        try
        {
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}
